//
//  OMOrderProductsTableCell.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2017/3/9.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMOrderProductsTableCell.h"
#import "OMOrderDetailAppearace.h"
#import "OMOrderDetailModel.h"
#import "OMOrder.h"
#import "OMOrderSubmitProductTableCell.h"
@interface OMOrderProductsTableCell () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UILabel *elementLabel;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) OMOrder *order;

@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *priceDetailLabel;
@property (nonatomic, strong) UILabel *deliveryFeeLabel;
@property (nonatomic, strong) UILabel *deliveryFeeDetailLabel;
@property (nonatomic, strong) UILabel *couponLabel;
@property (nonatomic, strong) UILabel *couponDetailLabel;
@property (nonatomic, strong) UILabel *amountLabel;
@property (nonatomic, strong) UILabel *amountDetailLabel;
@end

@implementation OMOrderProductsTableCell

#define kOMOrderProductsInerCellHeight SM_SIZE(150)
#define kOMOrderProductsSectionFooterHeight SM_SIZE(0)
#define kOMOrderProductsBottomViewHeight SM_SIZE(240)
- (void)configure
{
    [super configure];


    _elementLabel = [UILabel new];
    _elementLabel.textColor = SM_blackColor;
    _elementLabel.font = OMODA_Font;
    _elementLabel.text = @"购物清单";
    [self.contentView addSubview:_elementLabel];
    [_elementLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(OMODA_INSETS.left));
        make.top.equalTo(@(OMODA_INSETS.top));
    }];

    _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = SM_whiteColor;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.sectionHeaderHeight = DBL_EPSILON;
    _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, DBL_EPSILON)];
    //        _tableView.backgroundColor = SM_lightBlueColor;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tableView.separatorColor = SM_naviShadowColor;
    _tableView.scrollEnabled = false;
    _tableView.rowHeight = kOMOrderProductsInerCellHeight;
    [self.contentView addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(self.elementLabel.mas_bottom).offset(SM_SIZE(30));
        make.height.equalTo(@0);
    }];

    UIView *bottomView = [UIView new];
    [self.contentView addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(@0);
        make.height.equalTo(@(kOMOrderProductsBottomViewHeight));
    }];

    UIView *line = [UIView new];
    line.backgroundColor = SM_naviShadowColor;
    [bottomView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView.mas_centerX);
        make.top.equalTo(@(SM_SIZE(25)));
        make.width.equalTo(@(.5f));
        make.bottom.equalTo(@(SM_SIZE(-25)));
    }];

    _priceLabel = [UILabel new];
    _priceLabel.font = SM_CFont(29);
    _priceLabel.textColor = HEXCOLOR(@"#9F9F9F");
    _priceLabel.text = @"原价总计";
    [bottomView addSubview:_priceLabel];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(30)));
        make.top.equalTo(@(SM_SIZE(30)));
    }];

    _deliveryFeeLabel = [UILabel new];
    _deliveryFeeLabel.font = SM_CFont(29);
    _deliveryFeeLabel.textColor = HEXCOLOR(@"#9F9F9F");
    _deliveryFeeLabel.text = @"配送费用";
    [bottomView addSubview:_deliveryFeeLabel];
    [_deliveryFeeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(30)));
        make.top.equalTo(self.priceLabel.mas_bottom).offset(SM_SIZE(30));
    }];

    _couponLabel = [UILabel new];
    _couponLabel.font = SM_CFont(29);
    _couponLabel.textColor = HEXCOLOR(@"#9F9F9F");
    _couponLabel.text = @"店铺优惠";
    [bottomView addSubview:_couponLabel];
    [_couponLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(30)));
        make.top.equalTo(_deliveryFeeLabel.mas_bottom).offset(SM_SIZE(30));
    }];

    _priceDetailLabel = [UILabel new];
    _priceDetailLabel.font = SM_CFont(29);
    _priceDetailLabel.textColor = SM_blackColor;
    _priceDetailLabel.text = @"￥104.70";
    [bottomView addSubview:_priceDetailLabel];
    [_priceDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(line.mas_left).offset(SM_SIZE(-30));
        make.top.equalTo(self.priceLabel.mas_top);
    }];

    _deliveryFeeDetailLabel = [UILabel new];
    _deliveryFeeDetailLabel.font = SM_CFont(29);
    _deliveryFeeDetailLabel.textColor = SM_blackColor;
    _deliveryFeeDetailLabel.text = @"￥5.00";
    [bottomView addSubview:_deliveryFeeDetailLabel];
    [_deliveryFeeDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(line.mas_left).offset(SM_SIZE(-30));
        make.top.equalTo(self.deliveryFeeLabel.mas_top);
    }];

    _couponDetailLabel = [UILabel new];
    _couponDetailLabel.font = SM_CFont(29);
    _couponDetailLabel.textColor = SM_blackColor;
    _couponDetailLabel.text = @"-￥2.00";
    [bottomView addSubview:_couponDetailLabel];
    [_couponDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(line.mas_left).offset(SM_SIZE(-30));
        make.top.equalTo(self.couponLabel.mas_top);
    }];

    UIView *amountContainer = [UIView new];
    [bottomView addSubview:amountContainer];
    [amountContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line.mas_right);
        make.right.equalTo(@(SM_SIZE(0)));
        make.centerY.equalTo(bottomView.mas_centerY);
    }];

    _amountLabel = [UILabel new];
    _amountLabel.font = SM_CFont(29);
    _amountLabel.textColor = HEXCOLOR(@"#9F9F9F");
    _amountLabel.text = @"订单金额";
    [amountContainer addSubview:_amountLabel];
    [_amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.centerX.equalTo(amountContainer.mas_centerX);
    }];

    _amountDetailLabel = [UILabel new];
    _amountDetailLabel.font = SM_CFont(40);
    _amountDetailLabel.textColor = HEXCOLOR(@"#FA3D00");
    _amountDetailLabel.text = @"￥107.70";
    [amountContainer addSubview:_amountDetailLabel];
    [_amountDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.amountLabel.mas_bottom).offset(SM_SIZE(8));
        make.bottom.equalTo(@0);
        make.centerX.equalTo(self.amountLabel.mas_centerX);
    }];
}

- (void)configureCellWithObj:(OMOrder *)order
{
    _order = order;
    if ([NSThread isMainThread]) {
        [self reloadUI];
    }else {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self reloadUI];
        });
    }
}

- (void)reloadUI
{
    self.priceDetailLabel.text = [NSString stringWithFormat:@"￥%.2f",self.order.price.floatValue];
    self.deliveryFeeDetailLabel.text = [NSString stringWithFormat:@"￥%.2f",self.order.fee];
    self.couponDetailLabel.text = [NSString stringWithFormat:@"-￥%.2f",self.order.couponDiscount+self.order.promotionDiscount];
    self.amountDetailLabel.text = [NSString stringWithFormat:@"￥%.2f",self.order.amount.floatValue];
    [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@((kOMOrderProductsSectionFooterHeight+kOMOrderProductsInerCellHeight)*self.order.orderItems.count));
    }];
    [self.tableView reloadData];
}

#pragma mark - tableview delegate && datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.order.orderItems.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OMOrderSubmitProductTableCell *cell = [OMOrderSubmitProductTableCell tableViewCellWithTableView:tableView];
    [cell configureCellWithObj:self.order.orderItems[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return kOMOrderProductsSectionFooterHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

+ (CGFloat)cellHeightWithProductNum:(NSUInteger)num
{
    return ((kOMOrderProductsSectionFooterHeight+kOMOrderProductsInerCellHeight)*num+SM_SIZE(110)+kOMOrderProductsBottomViewHeight);
}

@end
